/**
*
* 2007-11-3 上午11:31:18
* @author xuhuan
*/

package edu.ccut.saturn.studio.logic.model.commands;


import org.eclipse.gef.commands.Command;

import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDecisionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnFalseConnection;

public class SaturnFalseConnectionCommand extends Command {

	private static final String SATURN_COMMECTION_CONNECTION_CREATION = "connection creation";
	private SaturnFalseConnection saturnFalseConnection;
	private final int lineStyle;

	private final SaturnDecisionNode source;
	private Shape target;

	/**
	 *	Instantiate a command that can create a connection between two shapes.
	 * @param source the source endpoint (a non-null Shape instance)
	 * @param lineStyle the desired line style. See Connection#setLineStyle(int) for details
	 * @throws IllegalArgumentException if source is null
	 * @see Connection#setLineStyle(int)
	 */
	public SaturnFalseConnectionCommand(SaturnDecisionNode source, int lineStyle) {
		if (source == null) {
			throw new IllegalArgumentException();
		}
		setLabel(SATURN_COMMECTION_CONNECTION_CREATION);
		this.source = source;
		this.lineStyle = lineStyle;
	}
	public boolean canExecute() {
		if (source.equals(target)) {
			return false;
		}
//		for (Iterator<SaturnFalseConnection> iter = source.getSourceFalseConnections().iterator(); iter.hasNext();) {
//			SaturnFalseConnection conn = (SaturnFalseConnection) iter.next();
//			if (conn.getTarget().equals(target)) {
//				return false;
//			}
//		}
		return true;
	}

	public void execute() {
		saturnFalseConnection = new SaturnFalseConnection(source, target, lineStyle);
	}

	public void redo() {
		saturnFalseConnection.reconnect();
	}

	public void setTarget(Shape target) {
		if (target == null) {
			throw new IllegalArgumentException();
		}
		this.target = target;
	}

	public void undo() {
		saturnFalseConnection.disconnect();
	}

}
